home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9282 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.6 KB

  1. Path: news.mistral.co.uk!usenet
  2. From: mikebarnard@mistral.co.uk (Mike Barnard)
  3. Newsgroups: comp.os.msdos.programmer,comp.lang.c
  4. Subject: SUUMMARY - my structure and pointer problems cured.
  5. Date: Sat, 09 Mar 1996 02:06:51 GMT
  6. Organization: Mistral Internet (Brighton)
  7. Message-ID: <4hr0v5$4gf@news.mistral.co.uk>
  8. NNTP-Posting-Host: l61.mistral.co.uk
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11. Hi.
  12.  
  13. I recently posted about a function that would not recognise
  14. a structure and it's variables. Thanks to all who replied,
  15. and I have the problem fixed now. I hope this information
  16. may be of some use. Especially to newbies like me who don't
  17. know about these sorts of facts.
  18.  
  19. The intention...
  20.  
  21. Using Borland TC3, I had 3 functions. "Main", "Vpcmenu" and
  22. "Prntitem". "Main" printed a title box then called "vpcmenu"
  23. to display and get input about a main menu. This declared a
  24. structure to hold information about the individual menu
  25. items. I delared an array of 9 structures and a pointer to
  26. them, then initialised them with thier data. In a loop I
  27. passed the value of the pointer to the function "prntitem",
  28. whose job was to print the contents of the pointed-to
  29. structure on the screen in the correct place.
  30.  
  31. The problem...
  32.  
  33. I had a compiler error in the function "prntitem" where I
  34. tried to use the variables within the structure. The error
  35. said the variables were undeclared. I was also told that the
  36. pointer I passed was never used. I tried moving the
  37. structure declaration to global and just inside main but no
  38. dice; the function "prntitem" didn't recognise the
  39. structure.
  40.  
  41. The answer...
  42.  
  43. It was because I was using seperate source files for each of
  44. the functions. I assumed that because the structure had been
  45. defined 'upwind' of the function that wanted to use it the
  46. compiler would just take the data from each file, make a
  47. program and the scope would be OK . Oh, no! In Borland at
  48. least (I don't know about other compilers) structure
  49. definitions, includes and god only knows what else is only
  50. valid for the source file it is in. Split into multiple
  51. source files and you have to repeat the declarations.
  52.  
  53. The cure...
  54.  
  55. Creating a header file with the information in it and
  56. including it in each and every source file. The following is
  57. a better explanation cut from a reply... my thanks to
  58. "tljones1" and all the others who replied.
  59.  
  60. -----------------------------------------------------------
  61. > files shouldn't matter? The compiler should sort that out shouldn't 
  62. > it? Maybe not...
  63.  
  64. nope... the compiler won't sort things out for you.  What is
  65. defined in one source file isn't in another.  Believe it or
  66. not, but it keeps you from running into problems.  You have
  67. to explicitly declare your types in each source file you
  68. intend to use them in.  and the best way to do that is to
  69. have a include file that declares that..  For example
  70. mystuff.h would have all of the declarations for the source
  71. file mystuff.cpp (so it has all of the structure
  72. declarations, global variables, function prototypes, etc.)
  73. and mystuff.cpp would include mystuff.h (it is also standard
  74. that mystuff.h has all of the other inncludes that
  75. mystuff.cpp would need).  Also another other source file
  76. that needed to use the types declared in mystuff.h would
  77. also include mystuff.h
  78.  
  79. I hope this makes sense... take it easy...
  80. -----------------------------------------------------------
  81.  
  82. If anything I've said is inaccurate, please publicly correct
  83. me. But this has worked for me. Now for the next bit,
  84. getting input and checking for specific keypresses. (Arrows,
  85. numbers and enter)... sigh. I'll get there in the end. With
  86. the help of some friends. Thanks.
  87.  
  88.  
  89. ---
  90. Mic.
  91. From windy, damp, but clearing skies Worthing; England.
  92. mikebarnard@mistral.co.uk
  93.  
  94.